using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using BUS;
using DAL;
using Utility;
using Encosia;

public partial class sanphamtheoDN : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == "")
        {
            Response.Redirect("dangnhap.aspx");
        }
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        int iD = int.Parse(Request.QueryString["iD"].ToString());

        Criteria cri = new Criteria();
        if (txt_tensp.Text != "")
        {
            tbsanphamcollection sanphamcollection = tbsanphamBUS.gettentheotensp(txt_tensp.Text,iD);
            Repeater1.DataSource = sanphamcollection;
        }
        else
        {
            tbsanphamcollection sanphamcollection = tbsanphamBUS.getidDN(iD);
            Repeater1.DataSource = sanphamcollection;
        }
        //int total = 0;
        
        Repeater1.DataBind();




        //Repeater1.DataSource = tbsanphamBUS.getidDN(iD);
        //Repeater1.DataBind();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            tbsanphamDTO sanpham = (tbsanphamDTO)e.Item.DataItem;
            HighslideImage hinh = (HighslideImage)e.Item.FindControl("img_hinh");


            tbhinhanhDTOColection hinhanh = tbhinhanhBUS.getid(sanpham.Masp);

            //for (int i = 0; i <= hinhanh.Count;i++ )
            //{

            //}
            hinh.ImageUrl = "~/Admin/image/hinhnho/" + hinhanh[0].Tenhinhanh;
            hinh.FullImageURL = "~/Admin/image/thump/" + hinhanh[0].Tenhinhanh;



            //hinh.TemplateSourceDirectory = "~/Admin/image/thump/" + hinhanh[0].Tenhinhanh.ToString();

            HyperLink hyp_ten = (HyperLink)e.Item.FindControl("hyp_ten");
            HyperLink h = (HyperLink)e.Item.FindControl("hyp_Sua");
            Literal lit_ma = (Literal)e.Item.FindControl("lit_ma");

            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");
            hf_id.Value = sanpham.Masp.ToString();
            lit_ma.Text = sanpham.Masp.ToString();

            hyp_ten.NavigateUrl = "~/Admin/sanphamct.aspx?id=" + sanpham.Masp.ToString();
            hyp_ten.Text = sanpham.Tensp.ToString();

            HyperLink hyp_sua = (HyperLink)e.Item.FindControl("hpl_Edit");
            hyp_sua.NavigateUrl = "~/Admin/sanphamsua.aspx?id=" + sanpham.Masp.ToString();

            tbloaispDTO loasp = tbloaisanphamBUS.GetByID(sanpham.Maloaisp);
            Literal tenloai = (Literal)e.Item.FindControl("lt_tenloai");
            tenloai.Text = loasp.Tenloaisp.ToString();
            tbdoanhnghiepDTO doanhnghiep = tbdoanhnghiepBUS.GetByID(sanpham.Madn);
            HyperLink tendn = (HyperLink)e.Item.FindControl("hp_tendn");
            tendn.Text = doanhnghiep.Tendn.ToString();
            tendn.NavigateUrl = "~/Admin/doanhnghiepct.aspx?id=" + sanpham.Madn.ToString();

            tbtrangthaiDTO tt = tbtrangthaiBUS.GetByID(sanpham.Matrangthai);
            Literal trangthai = (Literal)e.Item.FindControl("lt_trangthai");
            trangthai.Text = tt.Tentrangthai.ToString();

            tbtinhthanhDTO tthanh = tbtinhthanhBUS.GetByID(sanpham.Matinhthanh);
            Literal t_thanh = (Literal)e.Item.FindControl("lt_tinhthanh");
            t_thanh.Text = tthanh.Tentinhthanh.ToString();
        }
    }
    protected void btn_Timkiem_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void btn_xoa_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox cb = (CheckBox)item.FindControl("cb_xoa");
            if (cb.Checked == true)
            {

                HiddenField hf_id = (HiddenField)item.FindControl("hf_id");
                tbsanphamBUS.Delete(int.Parse(hf_id.Value));
            }
        }
        LoadData();
    }
}
